import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanDeactivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { BaseFormComponent } from '../modules/dynamic/base-form.component';
import { UserInfoService } from '../service/user-info/user-info.service';
import { setStorage } from '../utils/storage';

@Injectable({
  providedIn: 'root'
})
export class AuthGuardGuard implements CanActivate, CanDeactivate<BaseFormComponent> {
  constructor(
    private userInfo: UserInfoService
  ){}
  
  canDeactivate(component: BaseFormComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot | undefined): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    if(!this.userInfo.getIsLogin()){
      setStorage('redirect_url', currentState.url)
    }
    return true
  }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    
    return true;
  }
  
}
